package controller

import (
	"net/http"
	"text/template"

	"gitee.com/shuwenhe/shuwen-go/login/dao"
)

func Login(w http.ResponseWriter, r *http.Request) {
	t := template.Must(template.ParseFiles("views/pages/user/login.html"))
	t.Execute(w, "")
	username := r.PostFormValue("username")
	password := r.FormValue("password")
	user, _ := dao.CheckUsernameAndPassword(username, password)
	if user.ID > 0 {
		// 跳转登录成功页面
		t := template.Must(template.ParseFiles("views/pages/user/login_success.html"))
		t.Execute(w, user)
	} else {
		// 用户不存在，请先注册
		t := template.Must(template.ParseFiles("views/pages/user/regist.html"))
		t.Execute(w, "")
	}
}
